package com.dreamingwell.modestmap
{
    import com.modestmaps.Map;
    import com.modestmaps.events.MapEvent;
    import com.modestmaps.events.MarkerEvent;
    import com.modestmaps.geo.Location;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;

    public class PolygonMarker extends Sprite
    {
        private var points:Array = [];

        private var map:Map;
        
        private var panStart:Point;

        private var polySprite:Sprite = new Sprite();
        
        public var data:Object;


        public function PolygonMarker(map:Map,locations:Array)
        {
            this.mouseEnabled = true;
            this.mouseChildren = false;
            this.map = map;
            //this.blendMode = BlendMode.LIGHTEN;

			points = locations;

            addChild(polySprite);

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
            
            addEventListener(MouseEvent.ROLL_OVER, mouseRollOver);
            addEventListener(MouseEvent.ROLL_OUT,mouseRollOut);
            
        }
        
        private function mouseRollOver(event:Event):void {
        	redrawPoly(0xff9999);
        }
        
        private function mouseRollOut(event:Event):void {
        	redrawPoly();
        }
        
        private function onAddedToStage(event:Event):void 
        {
            onMapResize(null);
            map.addEventListener(MapEvent.START_PANNING, onMapStartPan);
            map.addEventListener(MapEvent.PANNED, onMapPanned);
            map.addEventListener(MapEvent.STOP_PANNING, onMapStopPan);
            map.addEventListener(MapEvent.ZOOMED_BY, onMapZoomedBy);
            map.addEventListener(MapEvent.STOP_ZOOMING, onEndZoom);
            map.addEventListener(MapEvent.EXTENT_CHANGED, onMapChange);
            map.addEventListener(MapEvent.RESIZED, onMapResize);
        }
        
        private function removedFromStage(event:Event):void {
            map.removeEventListener(MapEvent.START_PANNING, onMapStartPan);
            map.removeEventListener(MapEvent.PANNED, onMapPanned);
            map.removeEventListener(MapEvent.STOP_PANNING, onMapStopPan);
            map.removeEventListener(MapEvent.ZOOMED_BY, onMapZoomedBy);
            map.removeEventListener(MapEvent.STOP_ZOOMING, onEndZoom);
            map.removeEventListener(MapEvent.EXTENT_CHANGED, onMapChange);
            map.removeEventListener(MapEvent.RESIZED, onMapResize);        }
        
        public function onMapResize(event:Event):void
        {
            redrawPoly();
        }


        public function redrawPoly(fillColor:Number = 0xffffff):void 
        {        
     
           if(this.stage == null)
            	return;
            	

            polySprite.x = this.stage.stageWidth/2;
            polySprite.y = this.stage.stageHeight/2;
            
            polySprite.graphics.clear();
            
            var p1:Point = map.locationPoint(points[0],polySprite);
            polySprite.graphics.beginFill(fillColor,.5);

            polySprite.graphics.lineStyle(2,0x111111);
            polySprite.graphics.moveTo(p1.x, p1.y);
            for each (var location:Location in points.slice(1)) {
                var p:Point = map.locationPoint(location,polySprite);
                polySprite.graphics.lineTo(int(p.x),int(p.y));
            }
            polySprite.graphics.lineTo(p1.x, p1.y);
            polySprite.graphics.endFill();
        }

        private function onMapZoomedBy(event:MapEvent):void {
           
        }
        
        private function onEndZoom(event:MapEvent):void
        {
            redrawPoly();
        }

        private function onMapChange(event:MapEvent):void
        {
            redrawPoly();
        }
    
        private function onMapStartPan(event:MapEvent):void
        {
        }

        private function onMapStopPan(event:MapEvent):void
        {

        }
        
        private function onMapPanned(event:MapEvent):void
        {
        	if(panStart == null)
        		return;
        
        }

    }
}